package com.dingding.order.core.march.biz.impl;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dto.AbstractResponse;
import com.dingding.common.core.base.dto.ResponseEntity;
import com.dingding.order.code.order.PublishCarType;
import com.dingding.order.core.march.biz.IMarchOrderService;
import com.dingding.order.core.myorder.dao.IGetMyOrderDao;
import com.dingding.order.dto.march.underway.ReqHistoryOrder;
import com.dingding.order.dto.march.underway.ResUnderWayOrder;
import com.dingding.order.dto.march.underway.UderWayOrderDto;
import com.dingding.order.dto.myorder.ReqMyOrderDTO;
import com.dingding.order.dto.myorder.UserTakeInformation;
import com.dingding.order.processer.ProcesserFactory;

/**
 * @Description:进行中订单实现
 * @author:fengshuonan
 * @date: 2016年8月19日13:25:31
 */
@SuppressWarnings("all")
@Component("customerUnderWayOrderService")
public class CustomerUnderWayOrderServiceImpl extends AbstractResponse<ResUnderWayOrder, ReqHistoryOrder>
		implements IMarchOrderService {

	@Resource(name = "processerFactory")
	ProcesserFactory processerFactory;

	@Resource(name = "getMyOrderDao")
	IGetMyOrderDao getMyOrderDao;

	@Override
	@Transactional(readOnly = true)
	public String getMarchOrder(String requestDate) {

		ResponseEntity<ResUnderWayOrder> responseEntity = new ResponseEntity<ResUnderWayOrder>();
		ReqHistoryOrder requestHistoryOrder = super.parseRequestJson(requestDate);
		List<UderWayOrderDto> underWayOrders = new ArrayList<UderWayOrderDto>();

		// 验证参数
		if (!super.validateParam(responseEntity, requestHistoryOrder)) {
			return super.responseBeanToJson(responseEntity);
		}

		// 获取所有进行中订单(包括私家车，专车，出租车，后续如果开放别的车种类，在com.dingding.order.processer.impl包中增加实现)
		for (PublishCarType item : PublishCarType.values()) {
			List<UderWayOrderDto> orders = processerFactory.getOrderProcesser(item)
					.getUnderWayOrders(requestHistoryOrder.getUserId(), requestHistoryOrder.getClientType());
			for (UderWayOrderDto dto : orders) {
				underWayOrders.add(dto);
			}
		}

		// 3.获得用户总搭乘和总接乘里程
		UserTakeInformation userTakeInformation = getMyOrderDao.getUserTakeImformation(requestHistoryOrder.getUserId());

		ResUnderWayOrder res = new ResUnderWayOrder();
		if (userTakeInformation != null) {
			res.setPickUpMeters(userTakeInformation.getPickUpMeters());
			res.setRideMeters(userTakeInformation.getRideMeters());
		}
		res.setOrderList(underWayOrders);
		responseEntity.setCode(HttpStatus.SC_OK);
		responseEntity.setPromptMessage("请求成功");
		responseEntity.setResponseBody(res);
		return super.responseBeanToJson(responseEntity);
	}

}
